﻿using VaccinationHost.Model;

namespace VaccinationHost.Extend
{
    public static class DaprResponseExtend
    {
        public static async Task<object> ToResult(this HttpResponseMessage? message)
        {
            if(message == null)
            {
                return new OpResult() { Code = 400, Message = "操作失败。" };
            }
            else
            {
                if(message.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return await message.Content.ReadAsStringAsync();
                }
                else
                {
                    var content = await message.Content.ReadAsStringAsync();
                    var opresult = content.ToObject<OpResult>();
                    if(opresult == null)
                    {
                        opresult = new OpResult() { Code = 400, Message = content };
                    }

                    return opresult;
                }
            }
        }
    }
}
